package com.example.imtoken.modules.main.wallet.manage

import android.content.Intent
import android.text.InputType
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.databinding.ActivityManageBinding
import com.example.imtoken.modules.main.wallet.manage.admin.AdminManageActivity
import com.example.imtoken.modules.private_import.PrivateImportActivity
import com.example.imtoken.modules.private_import.backup.PrivateBackupActivity
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog

class ManageActivity :
    BaseActivity<ManageViewModel, ActivityManageBinding>(ManageViewModel::class.java) {

    private var isAdvancedShow = false

    private var functionType = 0

    private val key by lazy {
        intent.getStringExtra("key")
    }

    override fun initLayoutId(): Int {
        return R.layout.activity_manage
    }

    override fun init() {
        initTitle()
        initClick()
        initObserve()
        if (key == null) {
            viewModel.getDefAccount()
        } else {
            viewModel.getDefAccount(key)
        }
    }

    private fun initObserve() {
        viewModel.getDefAccountData().observe(this) {
            dataBinding.entity = it
        }
        viewModel.isDeleteOk.observe(this) {
            if (it) {
                finish()
            }
        }
    }

    override fun finish() {
        setResult(200)
        super.finish()
    }

    private fun initClick() {
        dataBinding.llAdvanced.setOnClickListener {
            isAdvancedShow = !isAdvancedShow
            if (isAdvancedShow) {
                dataBinding.ivAdvanced.setImageResource(R.drawable.ic_baseline_keyboard_arrow_up_24)
                dataBinding.llAdvancedF.visibility = View.VISIBLE
            } else {
                dataBinding.ivAdvanced.setImageResource(R.drawable.ic_baseline_keyboard_arrow_down_24)
                dataBinding.llAdvancedF.visibility = View.GONE
            }
        }
        dataBinding.llCopyAddress.setOnClickListener {
            //复制地址
            ClipboardUtils.copyText(viewModel.getDefAccountData().value?.address)
            ToastUtils.showShort(R.string.copySuccess)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llAdminManage) {
            //管理身份钱包
            val intent = Intent(this, AdminManageActivity::class.java)
            intent.putExtra("key", key)
            startActivityForResult(intent, 0)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llAdvancedF1) {
            //导出KeyStore
            functionType = 2
            walletPass()
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llAdvancedF2) {
            //导出私钥
            functionType = 3
            walletPass()
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.llAdvancedF3) {
            functionType = 4
            walletPass()
        }
        dataBinding.include.ivHeadBack.setOnClickListener {
            this.finish()
        }
        dataBinding.llWalletName.setOnClickListener {
            MaterialDialog.Builder(this)
                .title(R.string.walletName)
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input(
                    viewModel.getDefAccountData().value?.name,
                    ""
                ) { _, input ->
                    if (input.toString().isNotEmpty()) {
                        viewModel.updateWalletName(input.toString())
                    } else {
                        ToastUtils.showShort(R.string.pleaseEnterContent)
                    }
                }
                .positiveText(R.string.confirm)
                .negativeText(R.string.cancel)
                .cancelable(false)
                .show()
        }
        dataBinding.llDeleteWallet.setOnClickListener {
            //移除钱包
            functionType = 1
            walletPass()
        }
    }

    fun walletPass() {
        MaterialDialog.Builder(this)
            .title(R.string.walletPass)
            .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
            .input(
                getString(R.string.inputWalletPassTips),
                ""
            ) { _, input ->
                if (input.toString().isNotEmpty()) {
                    if (viewModel.walletPass(input.toString())) {
                        when (functionType) {
                            1 -> {
                                //移除钱包
                                viewModel.deleteWallet()
                            }
                            2 -> {
                                //导出KeyStore
                                val intent = Intent(this, PrivateImportActivity::class.java)
                                intent.putExtra("importType", 1)
                                intent.putExtra("passWord", input.toString())
                                ActivityUtils.startActivity(intent)
                            }
                            3 -> {
                                //导出私钥
                                val intent = Intent(this, PrivateImportActivity::class.java)
                                intent.putExtra("importType", 2)
                                intent.putExtra("passWord", input.toString())
                                ActivityUtils.startActivity(intent)
                            }
                            4 -> {
                                //备份钱包
                                when (viewModel.getDefAccountData().value?.type) {
                                    4 -> {
                                        //导出私钥
                                        val intent = Intent(this, PrivateImportActivity::class.java)
                                        intent.putExtra("importType", 2)
                                        intent.putExtra("passWord", input.toString())
                                        ActivityUtils.startActivity(intent)
                                    }
                                    5 -> {
                                        //导出KeyStore
                                        val intent = Intent(this, PrivateImportActivity::class.java)
                                        intent.putExtra("importType", 1)
                                        intent.putExtra("passWord", input.toString())
                                        ActivityUtils.startActivity(intent)
                                    }
                                    else -> {
                                        //备份钱包
                                        val intent = Intent(this, PrivateBackupActivity::class.java)
                                        intent.putExtra(
                                            "mnemonic",
                                            viewModel.getDefAccountData().value?.mnemonic
                                        )
                                        ActivityUtils.startActivity(intent)
                                    }
                                }
                            }
                        }
                    }
                } else {
                    ToastUtils.showShort(R.string.pleaseEnterPassWord)
                }
            }
            .positiveText(R.string.confirm)
            .negativeText(R.string.cancel)
            .cancelable(false)
            .show()
    }

    private fun initTitle() {
        dataBinding.include.tvHeadTitles.text = getString(R.string.manage)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            if (key == null) {
                viewModel.getDefAccount()
            } else {
                viewModel.getDefAccount(key)
            }
        }
    }

}